//
//  ModifyPwdTableViewController.m
//  SDSY
//
//  Created by dale on 2017/3/26.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "ModifyPwdTableViewController.h"
#import "ModifyPwdTableViewCell.h"
#import "UserInfoModel.h"
#import "NSString+Category.h"

@interface ModifyPwdTableViewController ()


@end

@implementation ModifyPwdTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.title=@"修改密码";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finish) ];
    
    

    
}


//提交修改密码
-(void) finish{
    // 1.获取三个密码内容
    UITextField *oldPwd;           // 旧密码
    UITextField *newPwd1;          // 新密码
    UITextField *newPwd2;          // 确认新密码
    for (int i = 0; i < 3; ++i) {
        ModifyPwdTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        if (i == 0){
            // 旧密码
            oldPwd = cell.textfied;
        } else if (i == 1){
            // 新密码
            newPwd1 = cell.textfied;
        }else if (i == 2){
            // 新密码
            newPwd2 = cell.textfied;
        }
    }
    // 2.验证两个密码是否一致
    if (![newPwd1.text isEqualToString:newPwd2.text]){
        [SVProgressHUD showErrorWithStatus:@"新密码不一致,请从新输入"];
        newPwd1.text = @"";
        newPwd2.text = @"";
        return;
    }

    // 3.请求参数
    NSDictionary *parameters = @{@"tid": [UserInfoModel shareInstance].tid, @"token": [UserInfoModel shareInstance].token, @"new_pwd": [newPwd1.text MD5String], @"old_pwd": [oldPwd.text MD5String]};
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"/SDSY/changePwd"
                                        parameters:parameters
                                           success:^(NSDictionary *respondDictionary) {
                                               if([respondDictionary[@"code"] intValue] == 1) {
                                                   // 修改密码成功
                                                   [SVProgressHUD showSuccessWithStatus:@"修改密码成功"];
                                                   [self.navigationController popViewControllerAnimated:YES];
                                               } else {
                                                   // 修改密码失败
                                                   [SVProgressHUD showSuccessWithStatus:@"原密码错误,请重新输入"];
                                               }
                                           } failure:^(NSError *error) {

            }];


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"温馨提示:密码默认为工号,修改后请注意记住自己的密码，如果忘记密码，请联系管理员找回密码";
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   ModifyPwdTableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    
    if (cell==nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ModifyPwdTableViewCell" owner:nil options:nil];
        cell = [nibs lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.row==0) {
        cell.name.text=@"旧密码";
        cell.textfied.placeholder=@"请输入旧密码";
    }else if (indexPath.row==1){
        cell.name.text=@"新密码";
        cell.textfied.placeholder=@"请输入新密码";
    }else{
        cell.name.text=@"确认密码";
        cell.textfied.placeholder=@"请确认新密码";
    }
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}


@end
